﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.Data;
using ShootSearch.Indexers;
using ShootSearch.Util.Serializers;
using System.Diagnostics;

namespace ShootSearch.Tests.Indexers
{
    [TestFixture]
    public class HubbleIndexerTests
    {
        
        [Test]
        public void CreateTest()
        {
            string connStr = "Data Source=127.0.0.1;Initial Catalog=News;";
            string query = "select top 10 * from news where content match '北京' order by score desc";
            Hubble.SQLClient.SqlConnection conn = new Hubble.SQLClient.SqlConnection(connStr);
            conn.Open();

            Hubble.SQLClient.SqlCommand cmd = new Hubble.SQLClient.SqlCommand(query,conn);

            DataSet ds = cmd.Query();

            for (int i = 0; i < ds.Tables.Count; i++)
            {
                for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
                {
                    Console.WriteLine(ds.Tables[i].Rows[j][1]);
                }
            }

            conn.Close();

        
        }


        public void InsertTest()
        {
            string connStr = "Data Source=127.0.0.1;Initial Catalog=News;";
            string queryInsert = "Insert News([Title],[Content],[Url],[Time]) values('Title','Content','Url','1984-04-02 11:00:00')";
            string queryCheck = "select top 10 * from news where content match 'Content' order by score desc";
            string queryDelete = "delete * from news where content match 'Content'";
            
            Hubble.SQLClient.SqlConnection conn = new Hubble.SQLClient.SqlConnection(connStr);
            conn.Open();
            Hubble.SQLClient.SqlCommand cmd = new Hubble.SQLClient.SqlCommand(queryInsert, conn);
            cmd.ExecuteNonQuery();


            cmd = new Hubble.SQLClient.SqlCommand(queryCheck, conn);
            DataSet ds = cmd.Query();

            for (int i = 0; i < ds.Tables.Count; i++)
            {
                for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
                {
                    Console.WriteLine(ds.Tables[i].Rows[j][1]);
                }
            }

            cmd = new Hubble.SQLClient.SqlCommand(queryDelete, conn);
            cmd.ExecuteNonQuery();

            conn.Close();
   
        }


        public void ConfigTest()
        {
            HubbleIndexerConfig config = new HubbleIndexerConfig();
            config.Connection = "Data Source=127.0.0.1;Initial Catalog=News;";
            config.Table = "News";
            config.InterestedAttributes.Add(new HubbleIndexerConfig.HubbleIndexerAttribute("Title", true));
            config.InterestedAttributes.Add(new HubbleIndexerConfig.HubbleIndexerAttribute("Content", true));
            config.InterestedAttributes.Add(new HubbleIndexerConfig.HubbleIndexerAttribute("Url", true));
            config.InterestedAttributes.Add(new HubbleIndexerConfig.HubbleIndexerAttribute("Time", true) { Format = "yyyy-MM-dd HH:mm:ss" });

            string xml = XmlFormatter.ToXml(config);

            Debug.WriteLine(xml);

        }
    }
}
